package com.zelic.znn.functions;

import com.zelic.znn.functions.OutputFunction;

/**
 * @since 17/07/2011
 * @author zelic
 * Filename: TanhMethod.java
 * Description: Tanh output method
 */

/**
 * Tanh method is used to generate double value from -1 to 1 but steeper than bipolar sigmoid
 * @author zelic
 */
public class TanhFunction implements OutputFunction {
    
    /**
     * Calculate output with TANH method
     * @param net:array of nets of units
     * @return output array 
     */
    
    @Override
    public double[] calculateOutputArray(double[] net){
        int length = net.length;
        double[] result = new double[length];
        for (int i=0;i<length;i++){
            result[i] = ((Math.exp(net[i])-Math.exp(-net[i]))/(Math.exp(net[i])+Math.exp(-net[i])));
        }
        return result;
    }

    /**
     * Derivation of tanh function
     * @param number : value to calculate derivation
     * @return derivation in double
     */
    @Override
    public double getDerivation(double number) {
        return (1-Math.pow(number,2));
    }
}
